#include <standard/resourcemanager.h>
#include <standard/geometrymath.h>
#include <standard/notification.h>
#include <standard/texturebuffer.h>

#include "terrainhelperobject.h"
#include "terrain/terrainobject.h"
#include "terrain/terrainblock.h"
#include "scene/sceneobjectnavigationmap.h"




CTerrainHelperGridGeometry::CTerrainHelperGridGeometry( CTerrainObject* pTerrainObject ){
	m_pTerrainObject = pTerrainObject;
}
CTerrainHelperGridGeometry::~CTerrainHelperGridGeometry(){

}
void CTerrainHelperGridGeometry::InitGeometry( CRenderItem* pRenderItem ){

}
void CTerrainHelperGridGeometry::UpdateGeometry(){

}



CTerrainHelperGridType0Geometry::CTerrainHelperGridType0Geometry(CTerrainObject* pTerrainObject):
CTerrainHelperGridGeometry(pTerrainObject){

}
CTerrainHelperGridType0Geometry::~CTerrainHelperGridType0Geometry() {

}
void CTerrainHelperGridType0Geometry::InitGeometry( CRenderItem* pRenderItem ){
	uint16 _vIndexList[64] = {
		0,1,2,3,4,5,6,7,8,
		17,26,35,44,53,62,71,80,
		79,78,77,76,75,74,73,72,
		63,54,45,36,27,18,9,0,
	};
	m_rIndexBuffer = NEW CIndexBuffer(_vIndexList, 33, True, sizeof(uint16));
	int32 _nBlockSizeX, _nBlockSizeY;
	m_pTerrainObject->GetBlockDim(&_nBlockSizeX, &_nBlockSizeY);
	CTexture* _pTexture = RESOURCE_GETTEXTURE("UV_Grid_Sm.jpg");
	CTerrainBlock* _pBlock;
	for ( int32 j = 0; j < _nBlockSizeY; j++) {
		for (int32 i = 0; i < _nBlockSizeX; i++) {
			_pBlock = m_pTerrainObject->GetBlock(i, j);
			pRenderItem->CreateNormalDiffuseBatchData( _pTexture, _pBlock->m_rVertexBuffer.GetEntityData(), m_rIndexBuffer.GetEntityData() );
		}
	}

}
void CTerrainHelperGridType0Geometry::UpdateGeometry() {


}



CTerrainHelperGridType1Geometry::CTerrainHelperGridType1Geometry(CTerrainObject* pTerrainObject):
CTerrainHelperGridGeometry(pTerrainObject){

}
CTerrainHelperGridType1Geometry::~CTerrainHelperGridType1Geometry() {

}
void CTerrainHelperGridType1Geometry::InitGeometry( CRenderItem* pRenderItem ){
	uint16 _vIndexList[192] = {
		0,1,2,3,4,5,6,7,8,
		17,16,15,14,13,12,11,10,9,
		18,19,20,21,22,23,24,25,26,
		35,34,33,32,31,30,29,28,27,
		36,37,38,39,40,41,42,43,44,
		53,52,51,50,49,48,47,46,45,
		54,55,56,57,58,59,60,61,62,
		71,70,69,68,67,66,65,64,63,
		72,73,74,75,76,77,78,79,80,

		71,62,53,44,35,26,17,8,
		7,16,25,34,43,52,61,70,79,
		78,69,60,51,42,33,24,15,6,
		5,14,23,32,41,50,59,68,77,
		76,67,58,49,40,31,22,13,4,
		3,12,21,30,39,48,57,66,75,
		74,65,56,47,38,29,20,11,2,
		1,10,19,28,37,46,55,64,73,
		72,63,54,45,36,27,18,9,0,
	};
	m_rIndexBuffer = NEW CIndexBuffer(_vIndexList, 192, False, sizeof(uint16));

	int32 _nBlockSizeX, _nBlockSizeY;
	m_pTerrainObject->GetBlockDim(&_nBlockSizeX, &_nBlockSizeY);
	CTexture* _pTexture = RESOURCE_GETTEXTURE("UV_Grid_Sm.jpg");

	CTerrainBlock* _pBlock;
	for ( int32 j = 0; j < _nBlockSizeY; j++) {
		for (int32 i = 0; i < _nBlockSizeX; i++) {
			_pBlock = m_pTerrainObject->GetBlock(i, j);
			pRenderItem->CreateNormalDiffuseBatchData( _pTexture, _pBlock->m_rVertexBuffer.GetEntityData(), m_rIndexBuffer.GetEntityData() );
		}
	}
}
void CTerrainHelperGridType1Geometry::UpdateGeometry() {

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
CTerrainGridHelperObject::CTerrainGridHelperObject() {

}
CTerrainGridHelperObject::~CTerrainGridHelperObject() {

}
void CTerrainGridHelperObject::Init(CTerrainObject* pTerrainObject, int32 nShowMode ) {
	m_pTerrainObject = pTerrainObject;
	m_nGeometryMode = nShowMode;

	switch (nShowMode) {
	case 0: m_rTerrainHelperGeometry = NEW CTerrainHelperGridType0Geometry(pTerrainObject); break;
	case 1: m_rTerrainHelperGeometry = NEW CTerrainHelperGridType1Geometry(pTerrainObject); break;
	//case 2: m_rTerrainHelperGeometry = NEW CTerrainHelperGridType2Geometry(pTerrainObject); break;
	}

	m_rRenderItem = NEW CRenderItem(m_vWorldM);
	m_rRenderItem->m_eRenderType = 0;
	m_rRenderItem->m_bDepthWrite = True;
    m_rRenderItem->m_bDepthTest = False;
	m_rRenderItem->m_bCastShadow = False;
	m_rRenderItem->m_bReceiveShadow = False;
	m_rRenderItem->m_bFogEnable = False;
	m_rRenderItem->m_bLightEnable = False;
	m_rRenderItem->m_bLineMode = False;
	m_rRenderItem->m_eBlending = NormalBlending;
	m_rRenderItem->SetPrimitiveType(EPT_LINE_LIST);
	m_rRenderItem->m_bLightEnable = True;
	m_rTerrainHelperGeometry->InitGeometry( m_rRenderItem.GetEntityData() );


}
void CTerrainGridHelperObject::UpdateGeometry() {
	m_rTerrainHelperGeometry->UpdateGeometry();
}
int32 CTerrainGridHelperObject::GetRenderElementHandles(CRenderElement** vRenderElementHandle, int32 nMaxCount) {
	vRenderElementHandle[0] = m_rRenderItem->m_pRenderElement.GetEntityData();
	return 1;
}

